package model.dba;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;

import model.Clase2;

public class ClaseDAO extends JDBC {
	private static ClaseDAO claseDAO;

	private ClaseDAO() {
		super();
	}

	public static ClaseDAO getInstance() {
		if (claseDAO == null) {
			claseDAO = new ClaseDAO();
		}
		return claseDAO;
	}

	public int add(Clase2 clase) {
		try {
			// insert into clases (fecha, idCursada) values('2014/10/06 09:00:00.00',1);--inicio

			String queryString = "INSERT INTO clases (fecha, idCursada)" + " VALUES(?,?) ";
			preparedStatement = connection.prepareStatement(queryString);
			preparedStatement.setTimestamp(1, new Timestamp(clase.getFecha().getTimeInMillis()));
			preparedStatement.setLong(2, clase.getIdCursada());

			preparedStatement.executeUpdate();

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

		return getLastId();

	}

	public boolean update(Clase2 clase) {
		boolean state = false;
		try {
			String queryString = "UPDATE clases SET fecha=?, idCursada=? WHERE idClase=?";

			preparedStatement = connection.prepareStatement(queryString);

			preparedStatement.setTimestamp(1, new Timestamp(clase.getFecha().getTimeInMillis()));
			preparedStatement.setLong(2, clase.getIdCursada());
			preparedStatement.setLong(3, clase.getId());

			state = preparedStatement.executeUpdate() != 0 ? true : false;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null)
					preparedStatement.close();
			}

			catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();

			}
		}
		return state;
	}

	public boolean delete(Integer id) {
		boolean state = false;
		try {
			String queryString = "DELETE FROM clases WHERE idClase=?";

			preparedStatement = connection.prepareStatement(queryString);
			preparedStatement.setInt(1, id);
			state = preparedStatement.executeUpdate() != 0 ? true : false;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return state;

	}

	public Clase2 get(Integer idClase) {

		Clase2 area = new Clase2();
		try {
			String queryString = "SELECT * FROM clases where idClase = '" + idClase + "';";

			preparedStatement = connection.prepareStatement(queryString);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				area = populate();

			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (resultSet != null)
					resultSet.close();
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return area;
	}

	public Clase2 find(Integer idCursada) {

		Clase2 area = new Clase2();
		try {
			String queryString = "SELECT * FROM clases where idCursada ='" + idCursada + "' ;";

			preparedStatement = connection.prepareStatement(queryString);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {

				area = populate();

			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (resultSet != null)
					resultSet.close();
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return area;
	}

	public int getLastId() {
		int ret = 0;
		try {
			String queryString = "SELECT idClase FROM clases order by idClase DESC LIMIT 1  ";

			preparedStatement = connection.prepareStatement(queryString);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				ret = resultSet.getInt("idArea");

			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (resultSet != null)
					resultSet.close();
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

		return ret;

	}

	public List<Clase2> getAll() {
		List<Clase2> areaList = new ArrayList<Clase2>();

		try {
			String queryString = "SELECT * FROM clases ;";

			preparedStatement = connection.prepareStatement(queryString);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {

				areaList.add(populate());
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (resultSet != null)
					resultSet.close();
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return areaList;
	}
	
	public List<Clase2> findAll(Integer idCursada) {

		List<Clase2> areaList = new ArrayList<Clase2>();
		try {
			String queryString = "SELECT * FROM clases where idCursada = '" + idCursada + "' ;";

			preparedStatement = connection.prepareStatement(queryString);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {

				areaList.add(populate());

			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (resultSet != null)
					resultSet.close();
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return areaList;
	}

	private Clase2 populate() throws SQLException {

		Clase2 clase = new Clase2();
		clase.setId((int) resultSet.getLong("idCursada"));
		GregorianCalendar fecha = new GregorianCalendar();
		fecha.setTime(resultSet.getTimestamp("fecha"));
		clase.setFecha(fecha);
	
		return clase;

	}
}